package com.sugar.follow.up.module.business.service.patientsqpg;

import com.sugar.follow.up.framework.common.pojo.PageResult;
import com.sugar.follow.up.framework.common.util.object.BeanUtils;
import com.sugar.follow.up.module.business.controller.admin.patient.sqpg.vo.PatientSqpgPageReqVO;
import com.sugar.follow.up.module.business.controller.admin.patient.sqpg.vo.PatientSqpgSaveReqVO;
import com.sugar.follow.up.module.business.dal.dataobject.patientsqpg.PatientSqpgDO;
import com.sugar.follow.up.module.business.dal.mysql.patientsqpg.PatientSqpgMapper;
import com.sugar.follow.up.module.system.api.user.AdminUserApi;
import com.sugar.follow.up.module.system.api.user.dto.AdminUserRespDTO;
import org.springframework.stereotype.Service;
import org.springframework.validation.annotation.Validated;

import javax.annotation.Resource;
import java.util.List;

import static com.sugar.follow.up.framework.common.exception.util.ServiceExceptionUtil.exception;
import static com.sugar.follow.up.module.business.enums.ErrorCodeConstants.PATIENT_SQPG_NOT_EXISTS;

/**
 * 术前评估 Service 实现类
 *
 * @author Ysugar
 */
@Service
@Validated
public class PatientSqpgServiceImpl implements PatientSqpgService {

    @Resource
    private PatientSqpgMapper patientSqpgMapper;
    @Resource
    private AdminUserApi adminUserApi;

    @Override
    public Long createPatientSqpg(PatientSqpgSaveReqVO createReqVO, Long adminUserId) {
        // 获取医生
        AdminUserRespDTO user = adminUserApi.getUser(adminUserId);
        // 插入
        PatientSqpgDO patientSqpg = BeanUtils.toBean(createReqVO, PatientSqpgDO.class);
        patientSqpg.setDoctorId(adminUserId).setDoctorName(user.getNickname());
        patientSqpgMapper.insert(patientSqpg);

        // 返回
        return patientSqpg.getId();
    }

    @Override
    public void updatePatientSqpg(PatientSqpgSaveReqVO updateReqVO) {
        // 校验存在
        validatePatientSqpgExists(updateReqVO.getId());
        // 更新
        PatientSqpgDO updateObj = BeanUtils.toBean(updateReqVO, PatientSqpgDO.class);
        patientSqpgMapper.updateById(updateObj);
    }

    @Override
    public void deletePatientSqpg(Long id) {
        // 校验存在
        validatePatientSqpgExists(id);
        // 删除
        patientSqpgMapper.deleteById(id);
    }

    @Override
        public void deletePatientSqpgListByIds(List<Long> ids) {
        // 删除
        patientSqpgMapper.deleteByIds(ids);
        }


    private void validatePatientSqpgExists(Long id) {
        if (patientSqpgMapper.selectById(id) == null) {
            throw exception(PATIENT_SQPG_NOT_EXISTS);
        }
    }

    @Override
    public PatientSqpgDO getPatientSqpg(Long id) {
        return patientSqpgMapper.selectById(id);
    }

    @Override
    public PageResult<PatientSqpgDO> getPatientSqpgPage(PatientSqpgPageReqVO pageReqVO) {
        return patientSqpgMapper.selectPage(pageReqVO);
    }

}